package com.wzdigit.wms.wms.controller.inv;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.inv.SearchSealHeaderRequest;
import com.wzdigit.wms.basic.client.request.inv.UnSealGoodsRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.inv.SealDtlGoodsResponse;
import com.wzdigit.wms.basic.client.response.inv.SealDtlResponse;
import com.wzdigit.wms.basic.client.response.inv.SealHeaderResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.inv.UnSealGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 封库/解封明细货品清单 前端控制器
 * </p>
 *
 * @author 
 * @since 2021-06-05
 */
@RestController
@Api(tags = "解封")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class UnSealGoodsController {

	public static final String REQUEST_MODEL = "/unsealgoods";
	
	@Autowired
	private UnSealGoodsService unSealGoodsService;

	/**
	 * 【解封主档数据】输出
	 * @param dto
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "【解封主档数据】输出",notes = "【解封主档数据】输出")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchSealHeader")
    public PagerInfo<List<SealHeaderResponse>> searchSealHeader(
    		SearchSealHeaderRequest dto, Pager<?> pager,
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return unSealGoodsService.searchSealHeader(dto, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 【解封明细数据】输出
	 * @param sealHeaderId
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "【解封明细数据】输出",notes = "【解封明细数据】输出")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchSealDtl")
    public List<SealDtlResponse> searchSealDtl(
    		Integer sealHeaderId,
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return unSealGoodsService.searchSealDtl(sealHeaderId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 【解封SN数据】输出
	 * @param dto
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "【解封SN数据】输出",notes = "【解封SN数据】输出")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchSealDtlGoods")
    public PagerInfo<List<SealDtlGoodsResponse>> searchSealDtlGoods(
    		Integer sealDtlId, Pager<?> pager,
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return unSealGoodsService.searchSealDtlGoods(sealDtlId, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 解封
	 * @param dto
	 * @param bindingResult
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 * @throws Exception 
	 */
	@ApiOperation(value = "解封",notes = "解封")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/unSealGoods")
    public Result<String> sealGoods(
    		@RequestBody @Valid UnSealGoodsRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) throws Exception {
        try {
        	ValidatorUtil.validator(bindingResult);
            return unSealGoodsService.unSealGoods(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
}
